--#include "data\functions\Common\CommonFunc.lua" once

--[[
给奖励，进背包，背包满时发邮件
num:倍率
]]
function GiveCommonAwardsBagOrMail( sysarg, awards, num, mail, logId, logStr )
	local myRewards = GetAwardsByRate( awards, num )		--N倍
	if not myRewards then
		return nil
	end
	if IsBagGridEnough( sysarg, myRewards ) then
		GiveCommonAward( sysarg, myRewards, logId, logStr )
	else
		local actorId 	= Actor.getActorId( sysarg )
		Awards.GiveToMailById( actorId, myRewards, mail.title, mail.content, logId, logStr )
	end
	return myRewards
end

--[[
按星级给予通关奖励
num：份数，主要用于批量操作、扫荡等
awardType:1-按星级区分奖励，2-物品库抽取
giveType:
1-进背包，背包不足时通过邮件
2-直接通过邮件
mail：
	mail.title：邮件title
	mail.content：邮件content
]]
function GiveCommonPassAwardsByStar( sysarg, starAwards, star, num, awardType, giveType, mail, logId, logStr )
	--print("GiveCommonPassAwardsByStar, star="..star..", num="..num..", giveType="..giveType)
	local awards = nil
	if awardType == 1 then
		awards = starAwards[star]
	elseif awardType == 2 then
		local awardLib = starAwards[star]
		if not awardLib then
			return
		end
		awards =DrawItemsFromAwardLibByCond( sysarg, awardLib, false )
	end

	if not awards then
		return
	end

	local myRewards = GetAwardsByRate( awards, num )		--N倍
	if giveType == 1 then
		if IsBagGridEnough( sysarg, myRewards ) then
			GiveCommonAward(sysarg, myRewards, logId, logStr)
		else
			local actorId 	= Actor.getActorId( sysarg )
			if mail.title and mail.content then
				Awards.GiveToMailById( actorId, myRewards, mail.title, mail.content, logId, logStr )
			end
		end
	else
		local actorId 	= Actor.getActorId( sysarg )
		--print("GiveCommonPassAwardsByStar,  actorId="..actorId)
		if mail.title and mail.content then
			Awards.GiveToMailById( actorId, myRewards, mail.title, mail.content, logId, logStr )
		end
	end

	return awards
end

--评定星级
function MakeCommonPassStar( starRule, passTime )
	if not starRule then
		return 0
	end

	if passTime <= 1 then 	--通关时间绝对没可能少到这种程度
		return 0
	end

	local star = 0
	for k, needTime in pairs( starRule ) do
		if passTime <= needTime then
			star = k
		end
	end
	return star
end



--[[
将物品库抽物品
支持开服天数
awardLib=
{	--1星奖励
	{
		openServerDay = {1, 9999},
		awards = 
		{
			{ libIdx=2, num=1, },		--RefreshItemLibCfg（ItemRefreshLibConfig.lua）中的下标
			{ libIdx=3, num=1, },
			{ libIdx=4, num=1, },
		},
	},
},
]]
function DrawItemsFromAwardLibByCond( sysarg, awardLib, isSame )
	local lib = GetElemByOpenServerDay( awardLib )
	if not lib then
		return nil
	end

	local allItems = {}
	for i, awardLib in ipairs( lib.awards ) do
		local items = DrawItemsFromItemLib( sysarg, awardLib.libIdx, awardLib.num, isSame )
		Merge2Table( allItems, items )
	end
	return allItems
end

--[[
libIdx:RefreshItemLibCfg（ItemRefreshLibConfig.lua）中的下标
num:抽取的数量
isSame：是否允许重复抽取
返回：被抽取的物品列表
]]
function DrawItemsFromItemLib( sysarg, libIdx, num, isSame )
	--print("DrawItemsFromItemLib,  libIdx="..libIdx..", num="..num)
	local itemLib = RefreshItemLibCfg[libIdx]
	if not itemLib then
		return nil
	end

	return DistributeElemByActor(sysarg, itemLib.lib, num, isSame)
end

